import { getCancelOrder, getDelOrder } from "@/apis/consult"
import { getMedicalOrderDetail } from "@/apis/order"
import { OrderType } from "@/enums"
import type { ConsultOrderItem } from "@/types/consult"
import type { OrderDetail } from "@/types/order"
import { showFailToast, showSuccessToast } from "vant"
import { onMounted, ref } from "vue"
//取消订单
export const useCancelOrder =()=>{
  //取消按扭
const cancelLoading = ref(false)
const onCancel = async (item: ConsultOrderItem) => {
  try {
    cancelLoading.value = true
    await getCancelOrder(item.id)
    item.status = OrderType.ConsultCancel
    item.statusValue = '已取消'
    showSuccessToast('取消订单成功')
  } catch (error) {
    showFailToast('取消订单失败')
  } finally {
    //关闭loading效果
    cancelLoading.value = false
  }
}
return {
  cancelLoading,
  onCancel
}
}
//删除订单
export const useDeleteOrder = (callback: () => void)=>{
  //删除订单
const delLoading = ref(false)
const onDelete = async (item: ConsultOrderItem) => {
  try {
    delLoading.value = true
    await getDelOrder(item.id)
    showSuccessToast('删除订单成功')
    callback&&callback()
  } catch (error) {
    showFailToast('删除订单失败')
  } finally {
    //关闭loading效果
    delLoading.value = false
  }
}
return {
  delLoading,
  onDelete
}
}

//药品订单详情
export const useOrderDetail = (id:string)=>{
  const medicalOrderDetail = ref<OrderDetail>()
  onMounted(async()=>{ 
    const res = await getMedicalOrderDetail(id)
    medicalOrderDetail.value = res.data}) 
return {
  medicalOrderDetail
}
}